home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
kermit.columbia.edu
/
kermit.columbia.edu.tar
/
kermit.columbia.edu
/
newsgroups
/
misc.20031118-20041115
/
000211_thucdat@hotmail.com_Wed Apr 14 14:31:34 2004.msg
< prev
next >
Wrap
Lisp/Scheme
|
2004-11-14
|
4KB
|
214 lines
Path: newsmaster.cc.columbia.edu!panix!news.maxwell.syr.edu!postnews1.google.com!not-for-mail
From: thucdat@hotmail.com (Dat Nguyen)
Newsgroups: comp.protocols.kermit.misc
Subject: Object-Oriented Programming in C-Kermit
Date: 14 Apr 2004 11:01:10 -0700
Organization: http://groups.google.com
Lines: 198
Message-ID: <6b1f50ac.0404141001.71ef5b14@posting.google.com>
NNTP-Posting-Host: 24.118.27.71
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1081965670 8379 127.0.0.1 (14 Apr 2004 18:01:10 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 14 Apr 2004 18:01:10 +0000 (UTC)
Xref: newsmaster.cc.columbia.edu comp.protocols.kermit.misc:14924
Yesteryear many software went object-oriented (even Pascal, Cobol and
Assembler). Today Patterns is the way to go.
C-Kermit does not provide built-in OOP environment, but its dynamic
evaluation of expressions can be exploited to do OOP and Patterns.
The following script provides the OOP environment in C-Kermit (8.x):
ftp://kermit.columbia.edu/kermit/scripts/ckermit/class
In this installment I use C-Kermit to implement the BankAccount class
described by John Maloney in:
http://www.squeak.org/tutorials/BankAccount.html
In Smalltalk, the BankAccount class is defined as:
Object subclass: #BankAccount
instanceVariableNames: 'balance'
classVariableNames: ''
poolDictionaries: ''
category: 'My Stuff'
In C-Kermit, with the support of the script 'class', the BankAccount
class is defined as:
class BankAccount
In C-Kermit, the instanceVariable 'balance' is created and initialized
with 0 when an object of BankAccount is instantiated with the class
method:
define BankAccount::new: {
_asg \%1.balance 0
(\%1)
}
In Smalltalk, a new object of BankAccount is created as follows:
b := BankAccount new
In C-Kermit:
BankAccount new: b
The Smalltalk method to retrieve the balance is:
balance
"Return the balance"
^ balance
In C-Kermit:
define BankAccount>>balance {
(\%1.balance)
}
and Smalltalk retrieves the balance of the account b as follows:
b balance
In C-Kermit:
b balance
To store that balance in a variable theBalance in C-Kermit:
(setq theBalance (b 'balance))
The Smalltalk method to deposit an amount into an account is:
deposit: amount
balance _ balance + amount.
in C-Kermit:
define BankAccount>>deposit: {
(++ \%1.balance \%2)
(\%1)
}
and Smalltalk deposits an amount 100 into the account b as follows:
b deposit: 100
in C-Kermit:
b deposit: 100
The Smalltalk method to withdraw an amount from an account is:
withdraw: amount
amount > balance ifTrue: [
^ self inform: 'sorry, not enough funds'].
balance _ balance - amount.
in C-Kermit:
define BankAccount>>withdraw: {
if < \m(\%1.balance) \%2 END -1 NOT ENOUGH BALANCE
(-- \%1.balance \%2)
(\%1)
}
and Smalltalk withdraws an amount 50 from the account b as follows:
b withdraw: 50
in C-Kermit:
b withdraw: 50
like in Smalltalk, in C-Kermit messages can be cascaded sending to an
object as follows:
((((BankAccount 'new: 'Bell) 'deposit: 500) 'withdraw: 150) 'balance)
which creates a new account Bell, deposits 500 to it, withdraws 150
from it and retrieves the balance from it.
To recap, follows is a rudimentary BankAccount application in
C-Kermit:
take class
class BankAccount
define BankAccount::new: {
_asg \%1.balance 0
(\%1)
}
define BankAccount>>balance {
(\%1.balance)
}
define BankAccount>>deposit: {
(++ \%1.balance \%2)
(\%1)
}
define BankAccount>>withdraw: {
if < \m(\%1.balance) \%2 END -1 NOT ENOUGH BALANCE
(-- \%1.balance \%2)
(\%1)
}
BankAccount new: Rich
(Rich 'balance)
Rich deposit: 1000000
(Rich 'balance)
Rich withdraw: 1
(Rich 'balance)
BankAccount new: Poor
(Poor 'balance)
Poor deposit: 5
(Poor 'balance)
Poor withdraw: 11
((((BankAccount 'new: 'Bell) 'deposit: 500) 'withdraw: 150) 'balance)
This program is object-oriented, very Smalltalk look alike. All
C-Kermit particular syntaxes are wrapped in the methods of the class,
and the new S-Espressions are very usefull for this style of
programming.
Dat Nguyen